package com.dpiinc.TechPage.widget

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import androidx.core.graphics.drawable.DrawableCompat
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.dpiinc.TechPage.R
import com.dpiinc.TechPage.utils.AppUtil
import com.beiji.lib.pen.constants.PaletteColor


/**
 * 调色板
 * Created by X on 2018/7/4.
 */
class PaletteView : RecyclerView {
    constructor(context: Context) : super(context){ initView() }
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs){ initView() }
    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle){ initView() }

    constructor(context: Context, defaultColorIndex: Int) : super(context){
        this.defaultColorIndex = defaultColorIndex
        initView()
    }

    private val colorList = ArrayList<PaletteColor>()

    var onColorPicked: ((color: PaletteColor) -> Unit)? = null
    private var defaultColorIndex = 0

    fun getSelectedColor(): PaletteColor {
        val selectedColorIndex = (adapter as PaletteAdapter).selectedColorIndex
        return if (selectedColorIndex >= 0 && selectedColorIndex < colorList.size) {
            colorList[selectedColorIndex]
        } else {
            colorList[0]
        }
    }

    private fun initView() {
        PaletteColor.values().forEach {
            colorList.add(it)
        }
        if (defaultColorIndex >= colorList.size){
            defaultColorIndex = 0
        }
        this.adapter = PaletteAdapter()
        this.setHasFixedSize(true)
        this.layoutManager = GridLayoutManager(context, SPAN_COUNT)
        this.setBackgroundResource(R.drawable.paletteview_bg_shape)
        setPadding(AppUtil.dp2px(10f).toInt(),0,AppUtil.dp2px(10f).toInt(),0)
//        this.addItemDecoration(SpaceItemDecoration())
//        this.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.HORIZONTAL).apply {
//            setDrawable(ContextCompat.getDrawable(context, R.drawable.paletteview_divider_shape)!!)
//        })
    }

    inner class PaletteAdapter : Adapter<PaletteItemViewHolder>() {
        var selectedColorIndex = defaultColorIndex

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PaletteItemViewHolder {
            val itemView = LayoutInflater.from(parent.context).inflate(R.layout.palette_item_layout, parent, false)
            return PaletteItemViewHolder(itemView)
        }

        override fun getItemCount(): Int {
            return colorList.size
        }

        override fun onBindViewHolder(holder: PaletteItemViewHolder, position: Int) {
            val color = colorList[position]
            holder.bindItem(color, selectedColorIndex == position)
            holder.itemView.setOnClickListener {
                selectedColorIndex = position
                notifyDataSetChanged()
                onColorPicked?.invoke(getSelectedColor())
            }
        }

    }

    inner class PaletteItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val colorView: View = itemView.findViewById(R.id.color_view)
        //        private val borderView: View = itemView.findViewById(R.id.border_view)
        private val colorSelectorView: View = itemView.findViewById(R.id.color_selector_view)

        fun bindItem(item: PaletteColor, isSelected: Boolean) {
            val wrappedDrawable = DrawableCompat.wrap(colorView.background as Drawable)
            DrawableCompat.setTint(wrappedDrawable, Color.parseColor(item.value))
            if (isSelected) {
                val padding = Math.round(AppUtil.dp2px(3f))
//                borderView.setPadding(padding, padding, padding, padding)
                colorSelectorView.visibility = View.VISIBLE
                colorView.layoutParams.height = AppUtil.dp2px(46f).toInt()
                colorView.layoutParams.width = AppUtil.dp2px(40f).toInt()
                colorSelectorView.layoutParams.height = AppUtil.dp2px(30f).toInt()
                colorSelectorView.layoutParams.width = AppUtil.dp2px(24f).toInt()
            } else {
//                borderView.setPadding(0, 0, 0, 0)
                colorSelectorView.visibility = View.INVISIBLE
                colorView.layoutParams.height = AppUtil.dp2px(42f).toInt()
                colorView.layoutParams.width = AppUtil.dp2px(36f).toInt()
            }
        }
    }

//    data class PaletteData(val id: Int, val value: String)

    companion object {

        //         The number of columns in the grid
        private const val SPAN_COUNT = 8

    }
}